using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Microsoft.Samples.Windows.Forms.Navigation
{
    /// <summary>
    /// A LinkLabel which displays the active link appearance on mouse over,
    /// not just when the link is being clicked. This LinkLabel control also
    /// absorbs clicks made with the mouse's X buttons so that these buttons
    /// can be reserved for use in navigating backwards and forwards.
    /// </summary>
	public class LinkLabel : System.Windows.Forms.LinkLabel
	{
		private Color saveLinkColor = Color.Empty;

		/// <summary>
		/// Switch to the hover color when the mouse enters the control.
		/// </summary>
		protected override void OnMouseEnter(EventArgs e)
		{
			base.OnMouseEnter(e);

			if (!DesignMode)
			{
				saveLinkColor = LinkColor;
				LinkColor = ActiveLinkColor;
			}
		}

		/// <summary>
		/// Absorb any mouse clicks made with the mouse's X buttons.
		/// </summary>
		/// <remarks>
		/// This prevents the LinkLabel's LinkClicked event from accidentally firing just because the mouse
		/// happened to be over the link when the user pressed one of the mouse's X buttons to move backward
		/// or forward.
		/// </remarks>
		protected override void OnMouseUp(MouseEventArgs e)
		{
			switch (e.Button)
			{
				case MouseButtons.XButton1:
				case MouseButtons.XButton2:
					// Click was made with the mouse's X buttons; absorb the click.
					break;

				default:
					base.OnMouseUp(e);
					break;
			}
		}

		/// <summary>
		/// Restore the normal link color when the mouse leaves the control.
		/// </summary>
		protected override void OnMouseLeave(EventArgs e)
		{
			base.OnMouseLeave(e);

			if (!DesignMode)
			{
				LinkColor = saveLinkColor;
			}
		}
	}
}
